//
//  ChangePwdViewController.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/8.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

class ChangePwdViewController: UIViewController {
    
    @IBOutlet weak var oldPsdTF: UITextField! {
        didSet {
            oldPsdTF.delegate = self
            oldPsdTF.addTarget(self, action: #selector(textfieldEditingChanged), for: .editingChanged)
        }
    }
    @IBOutlet weak var newPsdTF: UITextField! {
        didSet {
            newPsdTF.delegate = self
            newPsdTF.addTarget(self, action: #selector(textfieldEditingChanged), for: .editingChanged)
        }
    }
    @IBOutlet weak var reviewPsdTF: UITextField! {
        didSet {
            reviewPsdTF.delegate = self
            reviewPsdTF.addTarget(self, action: #selector(textfieldEditingChanged), for: .editingChanged)
        }
    }
    @IBOutlet weak var sureBtn: UIButton! {
        didSet {
            sureBtn.layer.cornerRadius = 25.0
            sureBtn.layer.masksToBounds = true
            sureBtn.setTitleColor(.white, for: .normal)
            sureBtn.setTitleColor(.main, for: .highlighted)
            sureBtn.setBackgroundImage(UIImage(color: .mainGray), for: .disabled)
            sureBtn.setBackgroundImage(UIImage(color: .mainColor), for: .normal)
            sureBtn.setBackgroundImage(UIImage(color: .mainGray), for: .highlighted)
        }
    }
    @IBOutlet weak var oldLine: UIView!
    @IBOutlet weak var newLine: UIView!
    @IBOutlet weak var reviewLine: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .background
    }
    
    @IBAction func sureChange() {
        if newPsdTF.text != reviewPsdTF.text {
            HUDTool.show(message: "两次新密码输入不相同")
            return
        }
        let oldPsd = oldPsdTF.text!.encryptWithAES()
        let newPsd = newPsdTF.text!.encryptWithAES()
        HUDTool.showActivity()
        ProfileViewModel.changePassword(old: oldPsd, new: newPsd) { (msg, code) in
            HUDTool.hideActivity()
            switch code {
            case .failure:
                HUDTool.showError(message: msg)
            case .success:
                UserDefaults.savePassword(newPsd)
                HUDTool.show(message: "密码修改成功")
                WebSocketManager.destroy()
                UserDefaults.removeUserInfo()
                self.navigationController?.popToRootViewController(animated: true)
            }
        }
    }

}

extension ChangePwdViewController: UITextFieldDelegate {
    @objc private func textfieldEditingChanged() {
        if oldPsdTF.text!.count > 0 &&
            newPsdTF.text!.count >= 6 &&
            reviewPsdTF.text!.count >= 6 {
            sureBtn.isEnabled = true
        } else {
            sureBtn.isEnabled = false
        }
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        // check for total length
        let totalLength = (textField.text?.lengthOfBytes(using: .utf8))! - range.length + string.lengthOfBytes(using: .utf8)
        if totalLength > 16 { return false }
        let lengthOfStr = string.lengthOfBytes(using: .utf8)
        for index in 0..<lengthOfStr {
            let chara = (string as NSString).character(at: index)
            // 48-57;{0,9};65-90;{A..Z};97-122:{a..z}
            if chara < 48 { return false }
            if chara > 57 && chara < 65 { return false }
            if chara > 90 && chara < 97 { return false }
            if chara > 122 { return false }
        }
        return true
    }
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        if textField == oldPsdTF {
            oldLine.backgroundColor = .splLineSelect
            newLine.backgroundColor = .splLineNormal
            reviewLine.backgroundColor = .splLineNormal
        } else if textField == newPsdTF {
            oldLine.backgroundColor = .splLineNormal
            newLine.backgroundColor = .splLineSelect
            reviewLine.backgroundColor = .splLineNormal
        } else {
            oldLine.backgroundColor = .splLineNormal
            newLine.backgroundColor = .splLineNormal
            reviewLine.backgroundColor = .splLineSelect
        }
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        oldLine.backgroundColor = .splLineNormal
        newLine.backgroundColor = .splLineNormal
        reviewLine.backgroundColor = .splLineNormal
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }
}
